﻿//画布相关操作
var CanvasHelper = function () { }

CanvasHelper.prototype = {
    //创建流程模版
    CreateTemplate: function () {
        var me = this;
        var flag = true;
        if (activityHelper != undefined)
            flag = confirm("确认新建？");
        if (flag) {
            if (activityHelper == undefined)
                activityHelper = new ActivityHelper();
            activityHelper.Dispose();
            //activitys = new Array();
            rules = new Array();

            var me = this;
            var po = StyleDefine.Window.height;
            //初始化节点
            var activity = activityHelper.AddActivity(new Point(50, 50), ActivityType.start);
            activityHelper.StartActivity = activity;
            var rule = new Rule(activity);
            activity = activityHelper.AddActivity(new Point(170, 50), ActivityType.drafting);
            activityHelper.DraftingActivity = activity;
            me.AddRule(rule, activity);
            activity = activityHelper.AddActivity(new Point($(document).width() - 150, $(document).height() - 100), ActivityType.end);
            activityHelper.EndActivity = activity;
            ToolBar.InitailSnapshot();
        }
    },
    //清空当前模版
    //Dispose: function () {
    //    activityHelper.Dispose();
    //},
    //添加规则
    /*
    rule：currentRule当前操作的规则
    activity：结束节点
    isload：调用此方法的位置
        1、从画布上手动绘制的
        2、打开或者导入流程时
        默认值 undefined
        当手动绘制时，添加规则操作会记录操作历史。
    */
    AddRule: function (rule, activity, isload) {

        gcommand = Command.NullOperation;
        if (rule.Points.length != 0) {
            var point = rule.GetIntersectionPoint(activity, rule.Points[rule.Points.length - 1]);
            rule.Points[rule.Points.length] = point;
        }
        else {
            var pointE = rule.GetIntersectionPoint(activity, rule.startActivity.position);
            var pointS = rule.GetIntersectionPoint(rule.startActivity, pointE);
            rule.Points[0] = pointS;
            rule.Points[1] = pointE;
        }
        rule.endActivity = activity;
        rule.startActivity.AddFromRule(rule);
        activity.AddToRule(rule);
        rules[rules.length] = rule;
        rule.ResetRule();
        if (isload == undefined)
            ToolBar.HistroyOpertion();
    },


    //生成一个新的画布
    InitialCanvas: function () {
        var canvas = $("<canvas>").css({ 'position': 'absolute', "z-index": "0", 'top': '0px', "left": "0px" }).attr({ "width": StyleDefine.Window.width, "height": StyleDefine.Window.height });
        var cxt = canvas[0].getContext("2d");
        $("#divWorkspace").append(canvas);
        return {
            'canvas': canvas,
            'cxt': cxt
        }
    },



    //获得鼠标当前位置
    GetPoint: function (e) {
        if (e != undefined) {
            if (e.offsetX != undefined)
                return new Point(e.offsetX, e.offsetY);
            else if (e.pageX != undefined) {
                return new Point(e.pageX - 36 + divWorkspace.scrollLeft(), e.pageY + divWorkspace.scrollTop());
            }
            else return e;
        } return e;
    },




    //根据鼠标位置获得选中的节点
    /*
    e：当前鼠标信息
    */
    GetSelectedActivity: function (e) {
        var selectedActivitys = new Array();
        var me = this;
        var point = me.GetPoint(e);
        $.each(activityHelper.Activitys, function (i, val) {
            if (val.Picture.isPointInPath(point.X, point.Y)) {
                selectedActivitys[selectedActivitys.length] = val;
            }

        });

        var result = {
            'isSelect': false,
            'selectedObject': null,
            'distinct': -1
        }


        if (selectedActivitys.length > 0) {
            var minDistinct = mathHelper.PointDistinct(selectedActivitys[0].position.X, selectedActivitys[0].position.Y, point.X, point.Y);
            result.selectedObject = selectedActivitys[0];
            result.distinct = minDistinct;
            result.isSelect = true;
            if (selectedActivitys.length > 1) {
                for (i = 1; i < selectedActivitys.length; i++) {
                    var dis = mathHelper.PointDistinct(selectedActivitys[i].position.X, selectedActivitys[i].position.Y, point.X, point.Y);
                    if (dis < minDistinct) {
                        dis = minDistinct;
                        result.selectedObject = selectedActivitys[i];
                        result.distinct = minDistinct;
                    }
                }
            }
        }
        return result;
    },

    //根据鼠标位置获得选中的规则
    /*
     e：当前鼠标信息
    */
    GetSelectedRule: function (e) {
        var me = this;
        var result = {
            'isSelect': false,
            'selectedObject': null,
            'distinct': 1000
        }
        $.each(rules, function (i, val) {
            var tmp = val.IsSelect(me.GetPoint(e));
            if (tmp.isSelect && result.distinct > tmp.distinct)
                result = tmp;
        });
        return result;
    },

    //重绘画布上的节点和规则样式
    ResetControl: function () {
        $.each(activityHelper.Activitys, function (i, val) { val.ResetActivity(); });
        $.each(rules, function (i, val) { val.ResetRule(); });
    },
    //重置画布上的节点和规则样式
    ClearAllStyle: function (e) {
        this.ClearActivityStyle(e);
        this.ClearRuleStyle();
    },

    RefreshAllStyle: function () {
        this.RefreshActivity();
        this.RefreshRule();
    },

    ClearActivityStyle: function (e) {
        $.each(activityHelper.Activitys, function (i, activity) { activity.ChangeSelectStatus(e); });
    },

    RefreshActivity: function (e) {
        var me = this;
        $.each(activityHelper.Activitys, function (i, activity) {
            activity.ChangeSelectStatus();
        });
    },

    RefreshRule: function () {
        $.each(rules, function (i, rule) { rule.ResetRule(); });
    },

    ClearRuleStyle: function () {
        $.each(rules, function (i, rule) { rule.ChangeSelectStatus(false); });
    },

    DrawMovePoint: function (cxt, point, length, style) {

        var x1 = point.X - length;
        var y1 = point.Y - length;
        cxt.restore();
        cxt.beginPath();
        cxt.setTransform(1, 0, 0, 1, 0, 0);
        cxt.strokeStyle = style;
        cxt.rect(x1, y1, 2 * length, 2 * length);
        cxt.stroke();
    },


    //绘制参考线
    /*
    position当前鼠标位置
    activityId 用来确定是节点移动时的捕捉还是绘制规则时的捕捉
    节点移动时activity为移动节点
    绘制规则时传入空值*/
    GetCaptureActivity: function (method, position, activity) {
        var me = activity;
        var capture = new Array();
        $.each(activityHelper.Activitys, function (i, val) {
            if (activity == undefined || val.guid != activity.guid) {
                var result = mathHelper[method](position, val.position)
                if (result.iscapture) {
                    result.activity = new Array();
                    result.activity[0] = val;
                    capture[capture.length] = result;
                }
            }
        });

        if (capture.length > 0) {
            var tmp = capture[0];

            if (capture.length > 1) {
                for (i = 1; i < capture.length; i++) {
                    if (capture[i].dis < tmp.dis)
                        tmp = capture[i];
                }

            }
            return tmp;
        }
        return null;
    },



    DrawFocus: function (cxt, point) {
        cxt.restore();
        cxt.beginPath();
        cxt.setTransform(1, 0, 0, 1, 0, 0);
        cxt.strokeStyle = "rgba(225,0,0,1)";
        cxt.moveTo(-5 + point.X, -5 + point.Y);
        cxt.lineTo(5 + point.X, 5 + point.Y);
        cxt.moveTo(5 + point.X, -5 + point.Y);
        cxt.lineTo(-5 + point.X, 5 + point.Y);
        cxt.stroke();
    },

    DrawFocusInvoke: function (cxt, activitys) {

        for (i = 0; i < activitys.length; i++) {
            this.DrawFocus(cxt, activitys[i].position);
        }
    }

}

//圆角矩形
CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r, shadow) {




    if (w < 2 * r) r = w / 2;
    if (h < 2 * r) r = h / 2;
    this.beginPath();

    //if (shadow != undefined) {
    //    this.save();
    //    this.shadowColor = "rgba(180,180,180,90)";
    //    this.shadowOffsetX = 4;
    //    this.shadowOffsetY = 4;
    //    this.shadowBlur = 3;
    //}
    if (isIe9) {
        x = -x;
        y = -y;


        this.moveTo(x, r - y);
        this.lineTo(x, y - r);
        this.arc(x - r, y - r, r, 0, Math.PI / 2);
        this.lineTo(r - x, y);
        this.arc(r - x, y - r, r, Math.PI / 2, Math.PI);
        this.lineTo(-x, r - y);
        this.arc(r - x, r - y, r, Math.PI, Math.PI * 1.5);
        this.lineTo(x - r, -y);
        this.arc(x - r, r - y, r, Math.PI * 1.5, Math.PI * 2);
        this.closePath();
        this.fillStyle = 'white';
        return this;

    }
    else {
        this.beginPath();
        this.moveTo(x + r, y);
        this.arcTo(x + w, y, x + w, y + h, r);
        this.arcTo(x + w, y + h, x, y + h, r);
        this.arcTo(x, y + h, x, y, r);
        this.arcTo(x, y, x + w, y, r);
        this.closePath();
        this.fillStyle = 'white';
        return this;
    }
}



CanvasRenderingContext2D.prototype.DrawLine = function (point1, point2, style) {

    this.setTransform(1, 0, 0, 1, 0, 0);
    this.strokeStyle = style
    this.beginPath();
    this.moveTo(point1.X, point1.Y);
    this.lineTo(point2.X, point2.Y);
    this.stroke();

}



CanvasRenderingContext2D.prototype.DrawCircle = function (x, y, r, style, isFill, shadow) {

    this.beginPath();
    this.save();
    //if (shadow != undefined) {

    //    this.shadowColor = "rgba(180,180,180,90)";
    //    this.shadowOffsetX = 2;
    //    this.shadowOffsetY = 2;
    //    this.shadowBlur = 3;
    //}
    this.arc(x, y, r, 0, Math.PI * 2, true);
    this.closePath();
    if (isFill == undefined) {
        this.fillStyle = style;
        this.fill();
    }
    else {
        this.strokeStyle = style;
        this.stroke();
    }
    this.restore();
}